import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_blobs
from sklearn.ensemble import RandomForestClassifier
repo_list = ["tensorflow", "netbeans", "phoenix", "Katello", "kuma", "moby", "opencv", "react", "scikit-learn", "terraform"]
for repo_name in repo_list:
    file_path = "./rank_data/" + repo_name + "/" + repo_name + "_svm_rank_format_data.txt"
    dir_path = "./rank_data/" + repo_name + "/"
    data = pd.read_table(file_path, sep=' ', header=None)
    b = data.drop(33, axis=1)
    b = b.drop(32, axis=1)
    b.columns = ['label', 'qid', 'is_core_member', 'is_merged', 'assignees', 'commits', 'lines_added', 'lines_deleted',
                 'labels', 'prev_CRRs',
                 'title_words', 'body_words', 'has_bug', 'has_document', 'has_feature', 'has_improve', 'has_refactor',
                 'commits_average', 'directories', 'subsystems', 'language_types', 'file_types', 'segs_added',
                 'segs_deleted',
                 'segs_changed', 'files_modified', 'file_developer', 'change_num', 'files_added', 'files_deleted',
                 'files_changed', 'has_test']
    #print(b)
    for index, row in b.iterrows():
        i = 1
        while i <= 31:
            b.loc[index, b.columns[i]] = b.loc[index, b.columns[i]].split(":")[1]
            i += 1
    #print(b)

    clf = RandomForestClassifier(n_estimators=10, max_depth=None, min_samples_split=2, random_state=0)
    x_train = b.iloc[:, 2:32]
    Y_train = b.iloc[:, 0]
    #print(x_train)
    clf.fit(x_train, Y_train)

    importances = clf.feature_importances_
    std = np.std([tree.feature_importances_ for tree in clf.estimators_],
                 axis=0)
    indices = np.argsort(importances)[::-1]

    # Print the feature ranking
    print("Project " + repo_name + " Feature ranking:")
    sum = 0
    for f in range(x_train.shape[1]):
        sum += importances[indices[f]]
        print("%d. feature %s (%f)" % (f + 1, x_train.columns[f], importances[indices[f]]))
    print("sum: %f", sum)
    # Plot the feature importances of the forest

    plt.figure()

    plt.title("Feature importances")

    plt.bar(range(x_train.shape[1]), importances[indices], color="r", yerr=std[indices], align="center")
    # plt.bar(range(x_train.shape[1]), importances[indices], color="g", align="center")
    # plt.xticks(range(x_train.shape[1]), [x_train.columns[i] for i in indices], rotation='45')
    plt.xticks(range(x_train.shape[1]), indices, rotation='45')

    plt.xlim([-1, x_train.shape[1]])
    #plt.savefig(dir_path + repo_name + ".png")
    plt.show()
